#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./login/bkaccount/validators.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from django.conf import settings
from bkaccount.accounts import Account
from bkaccount.models import BkUser
from bkaccount.constants import USERNAME_CHECK_PATTERN, PASSWORD_CHECK_PATTERN, CHNAME_CHECK_PATTERN, QQ_CHECK_PATTERN, PHONE_CHECK_PATTERN, EMAIL_CHECK_PATTERN

def validate_username(username):
    """
    检查用户名是否合法
    """
    if not USERNAME_CHECK_PATTERN.match(username):
        return (False, u'用户名【%s】错误，只能包含数字、字母和点，长度在4-20个字符' % username)
    return (True, '')


def validate_chname(chname):
    """
    检查用户中文名是否合法
    """
    if not CHNAME_CHECK_PATTERN.match(chname):
        return (False, u'中文名【%s】错误，只能包含数字、字母、中文汉字、下划线，长度在1-16个字符' % chname)
    return (True, '')


def validate_qq(qq):
    """
    检查QQ号
    """
    if not QQ_CHECK_PATTERN.match(qq):
        return (False, u'QQ【%s】错误，只能包含数字，长度在5-13个字符' % qq)
    return (True, '')


def validate_phone(phone):
    """
    检查手机号
    """
    if not PHONE_CHECK_PATTERN.match(phone):
        return (False, u'手机号【%s】错误，仅支持中国大陆手机号码（11位数字）' % phone)
    return (True, '')


def validate_email(email):
    """
    检查email
    """
    if not EMAIL_CHECK_PATTERN.match(email):
        return (False, u'email【%s】格式错误' % email)
    return (True, '')


def validate_password(password):
    """
    检查密码合法性
    """
    if not PASSWORD_CHECK_PATTERN.match(password):
        return (False, u'密码只支持数字、字母或!@#$%^*()_-+=，长度在8-20个字符，且必须保证包含大小写字母和数字')
    return (True, '')


def validate_twice_password(password1, password2):
    """
    检查2次密码一致性
    """
    if not all([password1, password2]):
        return (False, u'密码不能为空')
    if password1 != password2:
        return (False, u'两次输入的新密码不一致')
    return (True, '')


def validate_bk_token(data):
    """
    检查bk_token的合法性，并返回用户实例
    """
    account = Account()
    bk_token = data.get(account.BK_COOKIE_NAME)
    is_valid, token_data = account._is_bk_token_valid(bk_token)
    if not is_valid:
        return (False, None, token_data)
    try:
        user = BkUser.objects.get(username=token_data)
    except BkUser.DoesNotExist:
        return (
         False, None, u'用户%s不存在' % token_data)

    return (True, user, '')


def is_request_from_esb(request):
    """
    请求是否来自ESB
    """
    x_app_token = request.META.get('HTTP_X_APP_TOKEN')
    x_app_code = request.META.get('HTTP_X_APP_CODE')
    if x_app_code == 'esb' and x_app_token == settings.ESB_TOKEN:
        return True
    return False


def is_xls_valid(file_name):
    try:
        file_type = file_name.split('.')[-1]
    except:
        return (
         False, u'批量导入用户，解析文件名出错:%s' % file_name)

    if file_type not in ('xls', 'xlsx'):
        error_msg = u'文件格式错误，只支持：.xls 和 .xlsx 文件'
        return (
         False, error_msg)
    return (True, 'SUCCESS')
